/*
Author: Drew Risinger, Chris Cochoff, Jordan Walsh, Cameron Holloway
Course: COMP 220, Computer Programming II
Date: February 24, 2014
Description: This is the cpp file (implementation) for the Player class.
This class controls user action, displays hands, indicates if the player
has busted, and keeps track of the player's name.
*/
#include "Player.h"
#include <string>
#include <iostream>

using namespace std;

//define constructor
Player::Player(string name, PlayerType type){
	Player::name = name;
	Player::humanType = type;
}

/*	prompts player to choose what he wants to do on his turn: hit/stand?
	then calls appropriate action
*/
//This function is being called by a human
void Player::chooseHumanAction(Deck& masterDeck){
	char userAction;
	bool userInputViable = false;//cam - sentinel for viable input from user
	isStanding = false;
	while(userInputViable == false){
		displayHand();
		cout << name << ", what action would you like to take?" << endl
			 << "If you would like to hit, press 'h'," << endl
			 << "If you would like to stand/stay, press 's': " << endl;
		cout << "Your hand's current value is: " << handValue() << endl;
		cin >> userAction;
		if(userAction =='h'){
				system("cls");
				hit(masterDeck);
				cout << "Your hand's new value is: " << handValue() << endl;
				if(handValue()>21){
					cout << "You busted" << endl;
					stand();
					userInputViable = true;
				}
		}
		else if(userAction == 's'){
			stand();
			userInputViable = true;
			system("cls");
		}
		else{
			cout << "Please input a viable character, 'h' or 's': " << endl;
		}
	}
	
}

//This will be called if the "player" is a computer (or dealer)
void Player::chooseComputerAction(Deck& masterDeck){
	while(handValue() < 17)//Dealer will stand if hand is over 17, hit if under 17
	{
		hit(masterDeck);
	}
	stand();
	if(isBusted() == true) {
		cout<<"Dealer busted."<<endl;
	}
	else {
		cout << "dealer stands with: " << handValue() << endl;
	}
}

//Draws a card AND places it in hand
void Player::drawCard(Deck& masterDeck){
	hand.push_back(masterDeck.dealCard());
}

//Calls the drawCard function NO BUST CHECK INCLUDED
void Player::hit(Deck& masterDeck){
	if(humanType == Human)
		cout << "You've chosen to hit" << endl;
	drawCard(masterDeck);
}

//Player ends his play. isStanding set to true
void Player::stand(){
	isStanding = true;
	if(!isBusted())
		cout << "You've chosen to stand" << endl;
}

//returns value of cards in player's hand (i.e. 5 of spades +2 of hearts=7)
//For aces, we are assuming that an ace will start with a value of 
//11 and then become 1 if the score would be over 21
int Player::handValue(){
	int score=0;
	bool containsAce=false;
	for(unsigned int i=0; i < hand.size(); i++){
		score = score + hand.at(i).getCardValue();
		if(hand.at(i).getValue()==ace){
			containsAce=true;
		}
	}
	if(score>21 && containsAce){
		score-=10;	//subtracts the ace's value if would've busted.
	}
	return score;
}
//return player's name
string Player::getName(void){ 
	return name;
}

//outputs one card from the dealer's hand.
void Player::displayDealerHand(){
	cout << "Dealer shows:" << hand.at(1).name() << endl;
}


//outputs the cards in the player's hand.
void Player::displayHand(){
	cout<<"Your hand is: "<<endl;
	for(unsigned int i=0; i<hand.size(); i++){
		cout<< hand.at(i).name() <<endl;
	}
}


//Clears all cards in the player's hand. Used for playing multiple hands.
void Player::clearHand(){
	hand.clear();
}
